package cn.magic.org.controller;

import cn.magic.basic.annotation.PreAuthorize;
import cn.magic.basic.utils.AjaxResult;
import cn.magic.basic.utils.PageList;
import cn.magic.org.domain.Department;
import cn.magic.org.query.DepartmentQuery;
import cn.magic.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;


    /**
    * @Title: loadById
    * @Description: 通过id查询
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/6/24 10:00
    * @Parameters: [id]
    * @Return cn.magic.org.domain.Department
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department loadById(@ApiParam(value="部门ID主键",required = true) @PathVariable("id") Long id){
        return departmentService.loadById(id);

    }

    /**
    * @Title: loadAll
    * @Description: 查询所有
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/6/24 10:02
    * @Parameters: []
    * @Return java.util.List<cn.magic.org.domain.Department>
    */
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<Department> loadAll(){
        return departmentService.loadAll();
    }

    /**
    * @Title: remove
    * @Description: 通过id删除
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/6/24 11:06
    * @Parameters: [id]
    * @Return cn.magic.basic.utils.AjaxResult
    */
    @DeleteMapping("{id}")
    @ApiOperation(value = "通过ID删除" )
    // 自低定义注解获取权限
    @PreAuthorize(name="删除部门", sn = "department:delete")
    public AjaxResult remove(@ApiParam(value="部门ID主键",required = true) @PathVariable("id") Long id){
        try {
            departmentService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！！");
        }
    }

    /**
    * @Title: addOrUpt
    * @Description: 新增/修改
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/6/24 11:49
    * @Parameters: [department]
    * @Return cn.magic.basic.utils.AjaxResult
    */
    @PutMapping
    @ApiOperation(value = "新增或者修改" )

    public AjaxResult addOrUpt(@ApiParam(value="部门新增或者修改的对象",required = true)@RequestBody Department department){
        try {
            if (department.getId() == null){
                departmentService.save(department);
            } else{
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！！");
        }
    }

    /**
    * @Title: queryData
    * @Description: 高级查询和分页
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/6/24 11:57
    * @Parameters: [departmentQuery]
    * @Return cn.magic.basic.utils.PageList<cn.magic.org.domain.Department>
    */
    @ApiOperation(value = "高级查询和分页查询" )
    @PostMapping
    public PageList<Department> queryData(@ApiParam(value="高级查询和分页的对象",required = true)@RequestBody DepartmentQuery departmentQuery){
        PageList<Department> data = departmentService.queryData(departmentQuery);
        return data;

    }


    /**
    * @Title: batchDel
    * @Description: 批量删除
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/6/25 11:08
    * @Parameters: [ids]
    * @Return cn.magic.basic.utils.AjaxResult
    */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            departmentService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！！");
        }
    }

    /** 
    * @Title: tree
    * @Description: 获取部门树数据
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/6/25 21:54  
    * @Parameters: []
    * @Return java.util.List<cn.magic.org.domain.Department>
    */
    @GetMapping("/tree")
    public List<Department> tree(){
        return departmentService.tree();
    }





}
